package zfes.snowier.projects;




import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.zfes.snowier.boot.starter.configrations.datasourse.DynamicDataSourceRegister;
import org.zfes.snowier.boot.starter.configrations.swagger.EnableSnowierSwagger2;
import org.zfes.snowier.core.util.AppCtxUtil;
//@EnableAsync
//@EnableScheduling
//@EnableCaching
//@EnableAutoConfiguration

@ServletComponentScan
@EnableConfigurationProperties
@Import({DynamicDataSourceRegister.class})
@EnableSnowierSwagger2
@SpringBootApplication(scanBasePackages= {"org.zfes.snowier","zfes.snowier.projects"})
public class AppMainEntry{

    public static void main(String[] args) throws Exception {
    	AppCtxUtil.setAppContext(SpringApplication.run(AppMainEntry.class, args));
    	String[] names=AppCtxUtil.cfg.getBeanDefinitionNames();
    	for(String name: names) {
    		//System.out.println(name);
    	}
    	
    }
    @Bean  
	public FilterRegistrationBean filterRegistrationBean2() {  
	   FilterRegistrationBean filterRegistration = new FilterRegistrationBean();  
	   filterRegistration.setOrder(Ordered.HIGHEST_PRECEDENCE);
	   filterRegistration.setFilter(corsFilter());
	   filterRegistration.addUrlPatterns("/*");
	   filterRegistration.setName("crosFilter");
	   return filterRegistration;  
	} 
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
   
}
